from peewee import BigIntegerField, FloatField, DateTimeField, CharField, BooleanField

from common.model.abstrct import AbstractModelWithID, IntEnumField
from enums.client_user_enum import PointRecordTypeEnum


class ClientUserPoint(AbstractModelWithID):

    class Meta:
        table_name = "tb_client_user_point"
        indexes = (
            (("tenant_id", "user_id", ), True),
        )

    tenant_id = BigIntegerField(help_text="租户id")
    user_id = BigIntegerField(help_text="C端用户id")
    active_point = FloatField(help_text="可用积分", default=0)
    total_point = FloatField(help_text="总积分", default=0)
    used_point = FloatField(help_text="已用积分", default=0)


class ClientUserPointRecord(AbstractModelWithID):

    class Meta:
        table_name = "tb_client_user_point_record"
        indexes = (
            (("tenant_id", "user_id"), False),
            (("tenant_id", "request_id"), True),
            (("tenant_id", "redeem_id"), False)
        )

    tenant_id = BigIntegerField(help_text="租户id")
    user_id = BigIntegerField(help_text="c端用户id")
    point = FloatField(help_text="积分值,累积时是正数,消费时是负数,冲正时是被冲正的记录的积分取反")
    remark = CharField(max_length=16, help_text="积分记录备注")
    record_time = DateTimeField(help_text="消费时间")

    operate_type = IntEnumField(help_text="积分操作类型", choices=PointRecordTypeEnum)
    request_id = CharField(max_length=32, help_text="幂等id，表示某次操作")
    redeem_id = CharField(max_length=32, help_text="冲正的操作id,当operate_type=redeem时存在", null=True)
    redeemed = BooleanField(default=False, help_text="是否冲正,已经冲正的不能再次冲正，当前操作是冲正不能冲正")

